package com.acm.domain.email;

import static org.junit.Assert.*;

import java.util.Random;

import org.junit.Test;

import com.acme.domain.email.Email;
import com.acme.domain.email.Queue;
import com.acme.domain.email.impl.EmailImpl;
import com.acme.service.email.EmailService;
//import com.acme.service.email.EmailServiceTesterGetter;
//import com.acme.service.email.EmailServiceTesterPutter;

public class EmailServiceTester {

	@Test
	public void test() {
		new Thread(new EmailServiceTesterPutter()).start();
		new Thread(new EmailServiceTesterGetter()).start();
		System.out.println("Thread main is finished");

		assertEquals(1, 1);
	}

}

class EmailServiceTesterPutter implements Runnable {
	public void run() {
		EmailService service = EmailService.getEmailService();
		for (int i = 0; i < 20; i++) {
			if ((i + 1) % 15 == 0)
				service.close(); // close queue on 15 step
			Email email = new EmailImpl();
			email.setFrom("User_" + new Random().nextDouble());
			email.setTo("To_" + new Random().nextDouble());
			email.setSubject("Subject_" + new Random().nextDouble());
			email.setMessage("Message_" + new Random().nextDouble());
			service.sendNotificationEmail(email);
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		service.close();
	}
}

class EmailServiceTesterGetter implements Runnable {
	public void run() {
		try {
			EmailService service = EmailService.getEmailService();
			Queue<Email> queue = service.getQueue();
			Thread.sleep(6000);
			// service.close();
			for (int i = 0; i < 100; i++) {
				queue.getEmail();
				Thread.sleep(200);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
